package com.usth.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.jms.Session;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bcd.tools.DBUtils;
import com.usth.model.Airport;
import com.usth.model.BankCard;
import com.usth.model.BuyTVO;
import com.usth.model.FlightInfo;
import com.usth.model.SoldTicket;
import com.usth.model.Tickets;
import com.usth.model.TransitPrice;
import com.usth.model.User;

public class TicketsDaoImpl extends HibernateDaoSupport implements ITicketsDao {

	public void buytickets(BuyTVO bvo) {
		HttpSession session=ServletActionContext.getRequest().getSession();
		List sl=new ArrayList();
		User u=(User) session.getAttribute("user");
		String hql="from Airport where airportName=?";
		String hql2="from Tickets where flightId=?";
		Double d=new Double(0.0);
		for(SoldTicket st:bvo.getStlist()){
			if(st.getHname()!=null&&st.getHname().trim().length()!=0){
			st.setDate(bvo.getDate());
			st.setFlightId(bvo.getFvo().getFlightId());
			st.setStatus(1);//1预定 2付款 3出票
			st.setBeginAirpot(((Airport)((this.getHibernateTemplate().find(hql,bvo.getFvo().getTakeOffAirportName().trim())).get(0))).getAirportId());//机场编号
			st.setEndAirport(((Airport)((this.getHibernateTemplate().find(hql,bvo.getFvo().getLandingAirportName().trim())).get(0))).getAirportId());//机场编号
			String isTheWhole="";
			if(bvo.getPrice().equals(bvo.getFvo().getPrice())){
				isTheWhole="是";
			}else{
				isTheWhole="否";
			}
			st.setIsTheWhole(isTheWhole);//是否全程
			st.setUserId(u.getUserId());//购买人id
			st.setTiketId(((Tickets)((this.getHibernateTemplate().find(hql2,bvo.getFvo().getFlightId().trim()).get(0)))).getTiketId());//票id
			int i=(Integer) this.getHibernateTemplate().save(st);
			sl.add(i);
			d+=Double.parseDouble(bvo.getPrice());
			}
		}
		session.setAttribute("tprice", d);
		session.setAttribute("sl", sl);
	}

	public String pay(Double price,String card_id) {
		HttpSession session=ServletActionContext.getRequest().getSession();
		List sl=(List) session.getAttribute("sl");
		String r=null;
		Connection con=DBUtils.getconnection();
		Double d=new Double(-0.1);
		try {
			PreparedStatement pst=con.prepareStatement("select cash from bank_card where bank_id=?");
			pst.setString(1, card_id.trim());
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				d=rs.getDouble(1);
			};
			rs.close();
			pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if(d<price){
			r="余额不足，请换一张卡！";
		}else if(d==-0.1){
			r="卡号错误请确认后再次付款！";
		}else{
			try {
				PreparedStatement pst=con.prepareStatement("update bank_card set cash=(cash-?) where bank_id=?");
				pst.setDouble(1, price);
				pst.setString(2, card_id);
				pst.execute();
				pst.close();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
			r="付款成功！";
		
		for(int i=0;i<sl.size();i++){
			try {
				PreparedStatement pst1=con.prepareStatement("update sold_ticket set bank_id=?,statues=2 where sold_id=?");
				pst1.setString(1, card_id);
				pst1.setInt(2, (Integer) sl.get(i));
				pst1.execute();
				pst1.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}}
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		return r;
	}

	public List<SoldTicket> percp() {
		return this.getHibernateTemplate().find("from SoldTicket where statues=?",2);
	}

	@Override
	public void cp(int soldid) {
		Connection con=DBUtils.getconnection();
		try {
			PreparedStatement pst=con.prepareStatement("update sold_ticket set statues=? where sold_id=?");
			pst.setInt(1, 3);
			pst.setInt(2, soldid);
			pst.execute();
			pst.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<SoldTicket> pretp() {
		return this.getHibernateTemplate().find("from SoldTicket where statues=?",4);
	}

	public void tp(int soldid) {
		SoldTicket st=(SoldTicket) this.getHibernateTemplate().get("com.usth.model.SoldTicket", soldid);
		Tickets t=(Tickets) this.getHibernateTemplate().get("com.usth.model.Tickets", st.getTiketId());
		FlightInfo fi=(FlightInfo) this.getHibernateTemplate().get("com.usth.model.FlightInfo", t.getFlightId());
		if(st.getEndAirport()==Integer.parseInt(fi.getLandingAirport().trim())){
			BankCard bc=(BankCard) this.getHibernateTemplate().get("com.usth.model.BankCard", st.getBankId().trim());
			bc.setCash(bc.getCash()+fi.getTotalPrice());
			this.getHibernateTemplate().update(bc);
		}else{
			List<TransitPrice> list=this.getHibernateTemplate().find("from TransitPrice where flightId=? and airportId=?",new Object[]{fi.getFlightId().trim(),st.getEndAirport()});
			TransitPrice tp=list.get(0);
			BankCard bc=(BankCard) this.getHibernateTemplate().get("com.usth.model.BankCard", st.getBankId().trim());
			bc.setCash(bc.getCash()+tp.getPrice());
			this.getHibernateTemplate().update(bc);
		}
		st.setStatus(0);
		this.getHibernateTemplate().update(st);
	}

	public List<SoldTicket> userallt(int id) {
		return this.getHibernateTemplate().find("from SoldTicket where userId=?",id);
	}

	public void returnticket(int id) {
	SoldTicket st=	(SoldTicket) this.getHibernateTemplate().get("com.usth.model.SoldTicket", id);
	st.setStatus(4);
	this.getHibernateTemplate().update(st);
	}

	public void cancelreturn(int id) {
		SoldTicket st=	(SoldTicket) this.getHibernateTemplate().get("com.usth.model.SoldTicket", id);
		st.setStatus(2);
		this.getHibernateTemplate().update(st);
	}

	public Double payt(int id) {
		HttpSession session=ServletActionContext.getRequest().getSession();
		List sl=new ArrayList();
		SoldTicket st=	(SoldTicket) this.getHibernateTemplate().get("com.usth.model.SoldTicket", id);
		sl.add(st.getSoldId());
		session.setAttribute("sl", sl);
		FlightInfo fi=(FlightInfo) this.getHibernateTemplate().get("com.usth.model.FlightInfo", st.getFlightId().trim());
		if(fi.getLandingAirport().trim().equals(st.getEndAirport()+"")){
			return fi.getTotalPrice();
		}else{
			TransitPrice tp=(TransitPrice) this.getHibernateTemplate().find("from TransitPrice where flightId=? and airportId=?",new Object[]{fi.getFlightId(),st.getEndAirport()});
			return tp.getPrice();
		}
	}

}
